package com.zlc.workflow.core.process.controller;

import com.zlc.common.core.process.model.vo.StoneProcessClassVo;
import com.zlc.common.core.process.model.vo.StoneProcessVo;
import com.zlc.common.model.process.process.ProcessRequestParam;
import com.zlc.workflow.core.process.model.FlowNode;
import com.zlc.workflow.core.process.model.ProcessClassEntity;
import com.zlc.workflow.core.process.model.ProcessDeployEntity;
import com.zlc.workflow.core.process.model.WorkProcessEntity;
import com.zlc.workflow.core.process.service.ProcessService;
import com.zlc.workflow.flow.NodeModel;
import com.zlc.workflow.flow.NodeUtils;
import com.zlc.workflow.flow.TaskModel;
import com.zlc.workflow.flow.node.TaskNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Api(tags = "流程设计API")
@RestController
@Slf4j
@RequestMapping("/api/process")
public class ProcessController {

    @Autowired
    private ProcessService processService;

    /**
     * 查询流程定义
     */
    @ApiOperation(value = "查询流程定义")
    @GetMapping(value = "/queryList")
    public List<StoneProcessVo> queryList(ProcessRequestParam requestParam) {
        return processService.queryList(requestParam);
    }

    /**
     * 保存流程定义
     */
    @ApiOperation(value = "保存流程定义")
    @PostMapping(value = "/saveProcess")
    public ProcessDeployEntity saveProcess(@RequestBody ProcessDeployEntity entity) {
        return processService.saveProcess(entity);
    }

    /**
     * 查询流程设计详情实体
     */
    @ApiOperation(value = "查询流程设计详情实体")
    @GetMapping(value = "/getByProcessId")
    public ProcessDeployEntity getByProcessId(@RequestParam(value = "processId") String processId) {
        return processService.getByProcessId(processId);
    }

    /**
     * 查询出可发起的流程列表
     */
    @ApiOperation(value = "查询出可发起的流程列表")
    @GetMapping(value = "/queryWorkProcessList")
    public List<WorkProcessEntity> queryWorkProcessList() {
        return processService.queryWorkProcessList();
    }

    /**
     * 修改流程状态
     */
    @ApiOperation(value = "修改流程状态")
    @PostMapping(value = "/changeStatus")
    public void changeStatus(@RequestBody StoneProcessVo processVo) {
        processService.changeStatus(processVo.getProcessId(),processVo.getStatus());
    }

    @ApiOperation(value = "查询指定任务节点的前面所有节点")
    @GetMapping(value = "/getPreTaskNodes")
    public List<FlowNode> getPreTaskNodes(@RequestParam(value = "processId") String processId,
                                          @RequestParam(value = "nodeId") String nodeId) {
        TaskNode taskNode = NodeUtils.getTaskNode(processId, nodeId);
        if (ObjectUtils.isEmpty(taskNode)) {
            return new ArrayList<>();
        }
        List<NodeModel> allPreTaskNode = NodeUtils.getAllPreTaskNode(taskNode);
        return allPreTaskNode.stream().flatMap(nodeModel -> {
            FlowNode flowNode = new FlowNode();
            flowNode.setNodeId(nodeModel.nodeId());
            flowNode.setNodeNum(nodeModel.nodeNum());
            flowNode.setNodeName(nodeModel.nodeName());
            flowNode.setNodeType(nodeModel.nodeType().getValue());
            return Stream.of(flowNode);
        }).collect(Collectors.toList());
    }

    @ApiOperation(value = "查询指定流程的所有任务节点")
    @GetMapping(value = "/getAllTaskNodes")
    public List<FlowNode> getPreTaskNodes(@RequestParam(value = "processId") String processId) {
        List<NodeModel> allTaskNode = NodeUtils.getAllTaskNode(processId);
        return allTaskNode.stream().flatMap(nodeModel -> {
            FlowNode flowNode = new FlowNode();
            flowNode.setNodeId(nodeModel.nodeId());
            flowNode.setNodeNum(nodeModel.nodeNum());
            flowNode.setNodeName(nodeModel.nodeName());
            flowNode.setNodeType(nodeModel.nodeType().getValue());
            return Stream.of(flowNode);
        }).collect(Collectors.toList());
    }

    /**
     * 查询流程json实体
     */
    @ApiOperation(value = "查询流程json实体")
    @GetMapping(value = "/getProcessJsonByProcessId")
    public Map<String, String> getProcessJsonByProcessId(@RequestParam(value = "processId") String processId) {
        return processService.getProcessJsonByProcessId(processId);
    }

    /**
     * 类别树
     */
    @ApiOperation(value = "类别树")
    @GetMapping(value = "/classTree")
    public ProcessClassEntity classTree() {
        return processService.classTree();
    }

    /**
     * 添加类别
     */
    @ApiOperation(value = "添加类别")
    @PostMapping(value = "/addClass")
    public ProcessClassEntity addClass(@RequestBody StoneProcessClassVo classVo) {
        return processService.addClass(classVo);
    }

    /**
     * 保存类别
     */
    @ApiOperation(value = "保存类别")
    @PostMapping(value = "/saveClass")
    public void saveClass(@RequestBody StoneProcessClassVo classVo) {
        processService.saveClass(classVo);
    }

    /**
     * 删除类别
     */
    @ApiOperation(value = "删除类别")
    @GetMapping(value = "/removeClass")
    public void removeClass(@RequestParam(value = "classId") String classId) {
        processService.removeClass(classId);
    }


    @ApiOperation(value = "添加流程测试")
    @PostMapping(value = "/addProcessTest")
    public void addProcessTest(@RequestBody Map<String, String> map) {
        String processId = map.get("processId");
        String content = map.get("content");
        processService.addProcessTest(processId, content);
    }


}
